package com.cooper.scraper;

import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.util.Log;

public class ScraperBrowseParam {

	public static final int PAGE_NUMBER_PARAM = 1;
	public static final int ORDER_PARAM = 2;
	public static final int FILTER_PARAM = 3;
	public static final int SEARCH_KEYWORK = 4;
	public static final int OTHER_PARAM = 100;

	public static List<ScraperBrowseParam> browseParamsFactory(String xml)
			throws XmlPullParserException, IOException {

		List<ScraperBrowseParam> result = new ArrayList<ScraperBrowseParam>();
		XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
		factory.setNamespaceAware(true);
		XmlPullParser xpp = factory.newPullParser();
		xpp.setInput(new StringReader(xml));

		int eventType = xpp.getEventType();
		ScraperBrowseParam currentParam = null;
		String paramKey = null;
		while (eventType != XmlPullParser.END_DOCUMENT) {
			if (eventType == XmlPullParser.START_TAG) {
				if ("params".equals(xpp.getName())) {
					currentParam = new ScraperBrowseParam(
							Integer.valueOf(xpp.getAttributeValue(null,
									"position")),
							Integer.valueOf(xpp.getAttributeValue(null, "type")),
							xpp.getAttributeValue(null, "title"), xpp
									.getAttributeValue(null, "default"));
				} else if ("param".equals(xpp.getName())) {
					paramKey = xpp.getAttributeValue(null, "key");
				}
			} else if (eventType == XmlPullParser.TEXT) {
				if (currentParam != null && paramKey != null
						&& xpp.getText() != null) {
					currentParam.getParamList().put(paramKey, xpp.getText());
				}
			} else if (eventType == XmlPullParser.END_TAG) {
				if ("params".equals(xpp.getName())) {
					result.add(currentParam);
					currentParam = null;
				} else if ("param".equals(xpp.getName())) {
					paramKey = null;
				}
			}
			eventType = xpp.next();
		}

		return result;
	}

	private int type;

	private String key;

	private int position;

	private String title;

	private Map<String, String> paramList = new HashMap<String, String>(0);

	public ScraperBrowseParam(String searchKeyword) {
		super();
		this.type = SEARCH_KEYWORK;
		this.position = 2;
		this.key = searchKeyword;
	}

	public ScraperBrowseParam(int pageNumber) {
		super();
		this.type = PAGE_NUMBER_PARAM;
		this.position = 1;
		this.key = String.valueOf(pageNumber);
	}

	public ScraperBrowseParam(int position, int type) {
		super();
		if (position == 1 && type != PAGE_NUMBER_PARAM) {
			throw new IllegalArgumentException(
					"position 1: type must be PAGE_NUMBER_PARAM");
		}
		this.position = position;
		this.type = type;
	}

	public ScraperBrowseParam(int position, int type, String title,
			String defaultKey) {
		this(position, type);
		this.title = (title == null) ? "" : title;
		this.key = (defaultKey == null) ? "" : defaultKey;
	}

	public int getType() {
		return type;
	}

	public String getKey() {
		return key;
	}

	public String getValue() {
		String result = paramList.get(key);
		if (result == null) {
			return key;
		} else
			return result;
	}

	public String getEncodeKey(String charsetName) {
		try {
			if (charsetName == null || "".equals(charsetName.trim())) {
				return URLEncoder.encode(key, "utf-8");
			} else
				return URLEncoder.encode(key, charsetName);

		} catch (UnsupportedEncodingException e) {
			Log.e("ScraperBrowseParam.getEncodeKeyword", e.getMessage(), e);
			return key;
		}
	}

	public String getTitle() {
		return title;
	}

	public void setKey(String value) {
		this.key = value;
	}

	public int getPosition() {
		return position;
	}

	public Map<String, String> getParamList() {
		return paramList;
	}

	public void setParamList(Map<String, String> paramList) {
		this.paramList = paramList;
	}

}
